package com.pearadmin.system.controller;

import com.pearadmin.common.constant.ConfigurationConstant;
import com.pearadmin.common.tools.sequence.SequenceUtil;
import com.pearadmin.system.domain.SysConfig;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import com.pearadmin.common.constant.ControllerConstant;
import com.pearadmin.common.web.base.BaseController;
import com.pearadmin.common.web.domain.response.Result;
import com.pearadmin.system.domain.SysSetup;
import com.pearadmin.system.service.ISysConfigService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import javax.annotation.Resource;

/**
 * Describe: 设 置 控 制 器
 * Author: 就 眠 仪 式
 * CreateTime: 2019/10/23
 * */
@RestController
@RequestMapping(ControllerConstant.API_SYSTEM_PREFIX + "setup")
public class SysSetupController extends BaseController {

    private static String MODULE_PATH = "system/setup/";

    @Resource
    private ISysConfigService sysConfigService;

    @GetMapping("main")
    @PreAuthorize("hasPermission('/system/setup/main','sys:setup:main')")
    public ModelAndView main(Model model){
        SysSetup sysSetup = new SysSetup();
        SysConfig mailFromConfig = sysConfigService.getByCode(ConfigurationConstant.MAIN_FROM);
        SysConfig mailUserConfig = sysConfigService.getByCode(ConfigurationConstant.MAIN_USER);
        SysConfig mailPassConfig = sysConfigService.getByCode(ConfigurationConstant.MAIN_PASS);
        SysConfig mailHostConfig = sysConfigService.getByCode(ConfigurationConstant.MAIN_HOST);
        SysConfig mailPortConfig = sysConfigService.getByCode(ConfigurationConstant.MAIN_PORT);
        sysSetup.setMailFrom(mailFromConfig==null?"":mailFromConfig.getConfigValue());
        sysSetup.setMailUser(mailUserConfig==null?"":mailUserConfig.getConfigValue());
        sysSetup.setMailPass(mailPassConfig==null?"":mailPassConfig.getConfigValue());
        sysSetup.setMailHost(mailHostConfig==null?"":mailHostConfig.getConfigValue());
        sysSetup.setMailPort(mailPortConfig==null?"":mailPortConfig.getConfigValue());
        model.addAttribute("setup",sysSetup);
        return JumpPage(MODULE_PATH + "main");
    }

    @Transactional
    @PutMapping("save")
    @PreAuthorize("hasPermission('/system/setup/add','sys:setup:add')")
    public Result save(@RequestBody SysSetup sysSetup){
        updateSetup("邮箱来源",ConfigurationConstant.MAIN_FROM,sysSetup.getMailFrom());
        updateSetup("邮箱用户",ConfigurationConstant.MAIN_USER,sysSetup.getMailUser());
        updateSetup("邮箱密码",ConfigurationConstant.MAIN_PASS,sysSetup.getMailPass());
        updateSetup("邮箱端口",ConfigurationConstant.MAIN_PORT,sysSetup.getMailPort());
        updateSetup("邮箱主机",ConfigurationConstant.MAIN_HOST,sysSetup.getMailHost());
        return success("保存成功");
    }

    private void updateSetup(String name,String code,String value){
        SysConfig config = sysConfigService.getByCode(code);
        if(config !=null){
            config.setConfigValue(value);
            sysConfigService.updateById(config);
        }else{
            config = new SysConfig();
            config.setConfigId(SequenceUtil.makeStringId());
            config.setConfigName(name);
            config.setConfigCode(code);
            config.setConfigType("system");
            config.setConfigValue(value);
            sysConfigService.save(config);
        }
    }
}
